/* 197 */
/***/
function registerObject(g3v, guid, attachFunc, detachFunc) {

  var attachedObj = undefined;
  var delayAttach = undefined;

  if (g3v.objectsMap.has(guid)) {
    attachedObj = g3v.objectsMap.get(guid);
    attachFunc(attachedObj);
  }

  var disposer = g3v.objectOpsEvent.addEventListener(function(_ref) {
    var type = _ref.type,
      xbsjObj = _ref.xbsjObj;

    if (xbsjObj.guid === guid) {
      if (type === 'create') {
        if (attachedObj) {
          throw new Error('已经绑定过了！');
        }
        // 这里如果直接attachFunc的话，这个对象还没有完全创建完成。
        // 通过setTimeout的目的是让当前对象创建完成之后再做attachFunc操作
        // 否则会导致对于该对象的属性获取等失效
        delayAttach = setTimeout(function() {
          attachedObj = xbsjObj;
          attachedObj && attachFunc(attachedObj);
          delayAttach = undefined;
        }, 0);
      } else if (type === 'destroy') {
        // 保持attachFunc以后才可以调用detachFunc，避免调用了detachFunc，反而attachFunc没有调用的情况
        attachedObj && detachFunc(attachedObj);
        if (delayAttach) {
          clearTimeout(delayAttach);
          delayAttach = undefined;
        }
        attachedObj = undefined;
      }
    }
  });

  return function unregister() {
    attachedObj && detachFunc(attachedObj);
    disposer();
  };
}

export default registerObject;
